/**
 * Created by calan on 2018/11/14.
 */
var yearsSamePeriod = {
  html: function (timeTypeArray) {
    var timeType = {
      DayTimeType: '日',
      HouTimeType: '候',
      TenDaysTimeType: '旬',
      MonthTimeType: '月',
      SeasonTimeType: '季'
    };

    var curYear = new Date().getFullYear(),
      yearTemp = '<option value="">请选择年份</option>' + Array.apply(null, { length: 60 }).map(function(cur, ind){
        var year = curYear - ind;
        return '<option value="'+ year + '">'+ year +'</option>';
      }).join('');

    var timeTemp = timeTypeArray.filter(function (val) {return val !== 'HourTimeType' && val !== 'YearTimeType'})
      .reduce(function (all, cur) {
        return all += '<option value="'+ cur +'" >'+ timeType[cur] +'</option>';
      }, '');

    var timeTypeString = this.timeTemplate.dayTimeString();
    timeTypeString += this.timeTemplate.houTimeString();
    timeTypeString += this.timeTemplate.tenTimeString();
    timeTypeString += this.timeTemplate.monthTimeString();
    timeTypeString += this.timeTemplate.seasonTimeString();

    return '<div class="pluginsDiv" id="PeriodYearDiv">' +
      timeString({label: '年份选择:', displayType: 'block', display: true, className: 'selectPeriodYear pluginsSingleRows',
        templateArray: ['<select id="selectPeriodYear" multiple lay-omit lay-tools >'+ yearTemp +'</select>']}) +
      timeString({label: '时间类型:', displayType: 'block', display: true, className: 'time-type pluginsSingleRows',
        templateArray: ['<select lay-filter="periodTimeSelect" id="periodTimeSelect">'+ timeTemp +'</select>']}) +
      timeTypeString +
    '</div>';
  },

  renderCallback: function () {

    var curDate = new Date();
    var dt = new Date(curDate.getTime() - 24*60*60*1000);
    $('.periodDayTimeType .start-time, .periodDayTimeType .end-time').each(function(){
      layDate.render({ elem: this, format: 'MM月dd日', value: dt.format('MM月dd日')});
    });

    /*切换时间选项*/
    layForm.on('select(periodTimeSelect)', function (data) {
      /*显示相应的日期选择*/
      $('#PeriodYearDiv').children(':not(.time-type, .selectPeriodYear)').hide().siblings('.period' + data.value).show();
    });

  },

  getData: function () {
    var timeType = $('#periodTimeSelect').val();
    /*判断时间条件*/
    function timeJudge (start, end) {
      if ( start - end > 0 ) {
        layLayer.msg('开始时间不能大于结束时间', {icon: 7, time: 1500});
        Global.requestJudge = false;
      }
    }

    var start = '', end = '',
        startVal = '', endVal = '';

    switch (timeType) {
      case 'DayTimeType':

        startVal = $('.periodDayTimeType .start-time').val().replace(/[月日]/g, '');
        endVal = $('.periodDayTimeType .end-time').val().replace(/[月日]/g, '');

        timeJudge(startVal, endVal);

        start = startVal;
        end = endVal;

        break;
      case 'HouTimeType':
        startVal = $('.periodHouTimeType .start-time').val();
        endVal = $('.periodHouTimeType .end-time').val();

        timeJudge(startVal, endVal);

        start = startVal;
        end = endVal;
        break;
      case 'TenDaysTimeType':
        startVal = $('.periodTenDaysTimeType .start-time-month').val() + $('.periodTenDaysTimeType .start-time-ten').val();
        endVal = $('.periodTenDaysTimeType .end-time-month').val() + $('.periodTenDaysTimeType .end-time-ten').val();

        timeJudge(startVal, endVal);

        start = startVal;
        end = endVal;
        break;
      case 'MonthTimeType':
        startVal = $('.periodMonthTimeType .start-time').val();
        endVal = $('.periodMonthTimeType .end-time').val();

        timeJudge(startVal, endVal);

        start = startVal;
        end = endVal;
        break;
      case 'SeasonTimeType':
        startVal = $('.periodSeasonTimeType .start-time').val();
        endVal = $('.periodSeasonTimeType .end-time').val();

        timeJudge(startVal, endVal);

        start = startVal;
        end = endVal;
        break;
    }

    var years = $('#selectPeriodYear').val();
    Global.postData.years = years.length ? years : [new Date().getFullYear()];

    Global.postData.startTime = new Date().getFullYear() + start;
    Global.postData.endTime = new Date().getFullYear() + end;

    Global.postData.businessTimeType = timeType;
    Global.postData.requestDateTime = new Date().format('yyyy-MM-dd hh:mm:ss');
  },

  /*各个类型时间html*/
  timeTemplate: {
    dayTimeString: function () {
      var tem = timeString({label: '开始日期:', displayType: 'inline', display: true, className: 'periodDayTimeType pluginsSingleRows',
        templateArray:["<input class='layui-input start-time' type=\"text\"/>"]});
      tem += timeString({label: '结束日期:', displayType: 'inline', display: true, className: 'periodDayTimeType pluginsSingleRows',
        templateArray:["<input class='layui-input end-time' type=\"text\"/>"]});
      return tem;
    },
    houTimeString: function () {
      var tem = '',
        option = Array.apply(null, { length: 72 }).map(function(cur, ind){
          ind += 1;
          return '<option value="'+ ( ind > 9 ? ind : '0' + ind ) + '">'+ ind +'候</option>';
        }).join('');

      tem += timeString({label: '开始日期:', displayType: 'inline', display: false, className: 'periodHouTimeType pluginsSingleRows',
        templateArray:['<select  class="start-time">'+ option +'</select>']});
      tem += timeString({label: '结束日期:', displayType: 'inline', display: false, className: 'periodHouTimeType pluginsSingleRows',
        templateArray:['<select  class="end-time">'+ option +'</select>']});
      return tem;
    },
    tenTimeString: function () {
      var tem = '',
        tenOption = '',
        monthOption = Array.apply(null, { length: 12 }).map(function(cur, ind){
          ind += 1;
          return '<option value="'+ ( ind > 9 ? ind : '0' + ind ) + '">'+ ind +'月</option>';
        }).join('');
      ['上旬', '中旬', '下旬'].forEach(function (val, ind) {
        tenOption += '<option value="0'+ (ind+1) +'">'+ val +'</option>';
      });

      tem += timeString({label: '开始日期:', displayType: 'inline', display: false, className: 'periodTenDaysTimeType pluginsSingleRows',
        templateArray:['<select class="start-time-month">'+ monthOption +'</select>',
          '<select class="start-time-ten">'+ tenOption +'</select>']});
      tem += timeString({label: '结束日期:', displayType: 'inline', display: false, className: 'periodTenDaysTimeType pluginsSingleRows',
        templateArray:['<select class="end-time-month">'+ monthOption +'</select>',
          '<select class="end-time-ten">'+ tenOption +'</select>']});

      return tem;
    },
    monthTimeString: function () {
      var tem = '',
        monthOption = Array.apply(null, { length: 12 }).map(function(cur, ind){
          ind += 1;
          return '<option value="'+ ( ind > 9 ? ind : '0' + ind ) + '">'+ ind +'月</option>';
        }).join('');

      tem += timeString({label: '开始日期:', displayType: 'inline', display: false, className: 'periodMonthTimeType pluginsSingleRows',
        templateArray:['<select class="start-time">'+ monthOption +'</select>']});
      tem += timeString({label: '结束日期:', displayType: 'inline', display: false, className: 'periodMonthTimeType pluginsSingleRows',
        templateArray:['<select class="end-time">'+ monthOption +'</select>']});

      return tem;
    },
    seasonTimeString: function () {

      var tem = '',
        seasonOption = '';
      ['春季', '夏季', '秋季', '冬季'].forEach(function (val, ind) {
        seasonOption += '<option value="0'+ (ind+1) +'">'+ val +'</option>';
      });
      tem += timeString({label: '开始日期:', displayType: 'inline', display: false, className: 'periodSeasonTimeType pluginsSingleRows',
        templateArray:['<select class="start-time">'+ seasonOption +'</select>']});
      tem += timeString({label: '结束日期:', displayType: 'inline', display: false, className: 'periodSeasonTimeType pluginsSingleRows',
        templateArray:['<select class="end-time">'+ seasonOption +'</select>']});

      return tem;
    }
  }

};